// ignore_for_file: prefer_typing_uninitialized_variables

/*
 * @Author: yangni
 * @Date: 2023-09-11 08:39:08
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-10 16:39:05
 * @Description: 空调详情页
 */
import 'dart:async';
import 'dart:convert';
import 'package:get/get.dart';
import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/air_condition_detail/air_condition_detail_model.dart';
import 'package:yt_devops/pages/devices/device_tab/model/device_items_group_model.dart';
import 'package:yt_devops/pages/devices/device_tab/utils/device_func_util.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/device/device_sevices.dart';
import 'package:yt_devops/user_entity_manager.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/socket/use_subscribe.dart';

import '../base_detail_controller/base_detail_controller.dart';

class AirConditionDetailController extends BaseDetailController {
  AirConditionDetailModel model = AirConditionDetailModel();

  List<DeviceItemsGroupModel> groupItem = [];

  DeviceModel? deviceModel;

  String title = '';

  DeviceServices deviceServices = Get.find<DeviceServices>();

  /// scoket
  late Stream<Map<String, dynamic>> dataStream;

  UseSubsribe subsribe = UseSubsribe();

  /// 设备 id
  var deviceId;

  /// 产品id
  int? productId;

  /// 0-矩形(无设置功能)  1-比瓦特（运行及定值参数设置均有）
  /// 2-自研(不同角色设置不同)-【业主无定值参数设置】-【其余角色运行及定值参数设置均有】
  bool get hasAirSetting =>
      productId == ProductIDType.bWattAir.value ||
      productId == ProductIDType.yTEnergyAir.value;

  /// 是否业主
  bool get isOwner => UserEntityManager.instance.roleID == RoleType.owner.value;

  /// 是否从设备
  bool get isViceDevice =>
      deviceModel?.masterSlaveMode == MasterSlaveModeType.vice;

  /// 是否有参数设置
  bool get isShowSetting => hasAirSetting && !isViceDevice && !isOwner;

  @override
  void onInit() {
    super.onInit();

    inheritArguments();
  }

  /// 继承参数
  void inheritArguments() {
    deviceId = Get.arguments['deviceId'];
    title = Get.arguments['deviceName'];
    productId = Get.arguments['productId'];
    connectStatus = Get.arguments['connectStatus'];
    dataStream = subsribe.start([deviceId], true).stream.asBroadcastStream();
  }

  initData(DeviceModel? device) async {
    deviceModel = device;

    var list = await DeviceFuncUtils.getFormateByDeviceModel([
      model.controlItems,
      model.statusItems,
      model.runItems,
    ], deviceModel?.productId);

    groupItem = [
      DeviceItemsGroupModel(
          items: list[0], title: 'siteMonitor.controlInformation'.tr),
      DeviceItemsGroupModel(
          items: list[1], title: 'siteMonitor.statusInformation'.tr),
      if (hasAirSetting && !isViceDevice)
        DeviceItemsGroupModel(
            items: list[2], title: 'siteMonitor.runningParameterSet'.tr),
    ];
    update();
  }

  Map<String, dynamic> runningTimeFrameToList(String val) {
    var list = jsonDecode(val);
    Map<String, dynamic> map = {};
    for (var i = 0; i < list.length; i++) {
      map.putIfAbsent('runningTimeFrame${i + 1}', () => list[i]['TimeFrame']);
    }
    return map;
  }

  @override
  void onClose() {
    subsribe.closeListen();
  }
}
